#!/bin/bash
# 切换到脚本所在目录，确保Mac下双击执行时在正确的目录中
cd "$(dirname "$0")"

# 定义颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
GRAY='\033[0;37m'
NC='\033[0m' # 无颜色

# 获取项目根目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$(dirname "$(dirname "$SCRIPT_DIR")")" && pwd)"
FRONTEND_DIR="${PROJECT_ROOT}/hello-ui"
NGINX_DIR="${SCRIPT_DIR}"
DIST_DIR="${FRONTEND_DIR}/dist"

echo -e "${CYAN}脚本位置: ${SCRIPT_DIR}${NC}"
echo -e "${CYAN}项目根目录: ${PROJECT_ROOT}${NC}"
echo -e "${CYAN}前端目录: ${FRONTEND_DIR}${NC}"
echo -e "${CYAN}Nginx目录: ${NGINX_DIR}${NC}"

# --- 配置变量 ---
VERSION="v2.0.6" # 或者您想要的版本
REGISTRY="registry.cn-hangzhou.aliyuncs.com"
NAMESPACE="hello-cloud"
USERNAME="百映zzz"
PASSWORD="zxcZXC123"

SERVICE_NAME="hello-ui" 
CONTEXT_DIR="." 
DOCKERFILE_PATH="${CONTEXT_DIR}/dockerfile"
# --- 配置结束 ---

# 显示欢迎信息
echo -e "\n${YELLOW}====================================================${NC}"
echo -e "${YELLOW}        前端项目构建和镜像推送脚本${NC}"
echo -e "${YELLOW}====================================================${NC}"

# 先删除Nginx目录下的旧dist目录
if [ -d "${NGINX_DIR}/dist" ]; then
  echo -e "${YELLOW}删除Nginx目录下的旧dist目录...${NC}"
  rm -rf "${NGINX_DIR}/dist"
  echo -e "${GREEN}Nginx目录下的旧dist目录已删除${NC}"
fi

# ====================== 第1阶段：构建前端项目 ======================
echo -e "\n${YELLOW}[第1阶段] 开始构建前端项目...${NC}"
echo -e "${GRAY}----------------------------------------------------${NC}"

# 检查前端目录是否存在
if [ ! -d "${FRONTEND_DIR}" ]; then
  echo -e "${RED}错误: 前端项目目录不存在 (${FRONTEND_DIR})${NC}"
  echo -e "${GRAY}按Enter键退出...${NC}"
  read -r
  exit 1
fi

# 切换到前端目录
cd "${FRONTEND_DIR}"
echo -e "${CYAN}切换到前端目录: ${FRONTEND_DIR}${NC}"

# 先删除旧的dist目录
if [ -d "${DIST_DIR}" ]; then
  echo -e "${YELLOW}删除旧的dist目录...${NC}"
  rm -rf "${DIST_DIR}"
  echo -e "${GREEN}旧的dist目录已删除${NC}"
fi

# 询问用户选择构建方式
echo -e "${YELLOW}请选择构建方式:${NC}"
echo -e "${CYAN}1. K8S环境构建 (npm run build:k8s)${NC}"
echo -e "${CYAN}2. Docker环境构建 (npm run build:docker)${NC}"
echo -e "${YELLOW}请输入选项 [1-2]: ${NC}"
read -r BUILD_OPTION

# 根据用户选择执行不同的构建命令
if [ "$BUILD_OPTION" == "1" ]; then
  echo -e "${YELLOW}执行K8S环境构建...${NC}"
  npm run build:k8s
elif [ "$BUILD_OPTION" == "2" ]; then
  echo -e "${YELLOW}执行Docker环境构建...${NC}"
  npm run build:docker
else
  echo -e "${RED}无效的选项，将默认使用K8S环境构建${NC}"
  npm run build:k8s
fi

# 检查构建结果
if [ ! -d "${DIST_DIR}" ]; then
  echo -e "${RED}前端构建失败: dist目录不存在${NC}"
  echo -e "${GRAY}按Enter键退出...${NC}"
  read -r
  exit 1
fi

echo -e "${GREEN}前端构建完成${NC}"

# ====================== 第2阶段：复制dist目录 ======================
echo -e "\n${YELLOW}[第2阶段] 复制dist目录到Nginx...${NC}"
echo -e "${GRAY}----------------------------------------------------${NC}"

# 切换到Nginx目录
cd "${NGINX_DIR}"
echo -e "${CYAN}切换到Nginx目录: ${NGINX_DIR}${NC}"

# 复制新的dist目录
echo -e "${YELLOW}复制新的dist目录...${NC}"
cp -R "${DIST_DIR}" "${NGINX_DIR}/"

# 检查复制结果
if [ ! -d "${NGINX_DIR}/dist" ]; then
  echo -e "${RED}复制dist目录失败${NC}"
  echo -e "${GRAY}按Enter键退出...${NC}"
  read -r
  exit 1
fi

echo -e "${GREEN}dist目录复制完成${NC}"

# ====================== 第3阶段：构建和推送Docker镜像 ======================
echo -e "\n${YELLOW}[第3阶段] 构建和推送Docker镜像...${NC}"
echo -e "${GRAY}----------------------------------------------------${NC}"

# 登录阿里云镜像仓库
echo -e "${YELLOW}登录阿里云镜像仓库 (${REGISTRY})...${NC}"
docker login --username="${USERNAME}" "${REGISTRY}" --password="${PASSWORD}"

if [ $? -ne 0 ]; then
  echo -e "${RED}登录失败，请检查凭据。${NC}"
  echo -e "${GRAY}按Enter键退出...${NC}"
  read -r
  exit 1
fi

# 检查 Dockerfile 是否存在
if [ ! -f "${DOCKERFILE_PATH}" ]; then
  echo -e "${RED}错误: Dockerfile 不存在于 ${DOCKERFILE_PATH}${NC}"
  echo -e "${GRAY}按Enter键退出...${NC}"
  read -r
  exit 1
fi

# 构建镜像
echo -e "${YELLOW}开始构建 ${SERVICE_NAME}:${VERSION} 镜像...${NC}"

docker build --platform=linux/amd64 -t "${SERVICE_NAME}:${VERSION}" -f "${DOCKERFILE_PATH}" "${CONTEXT_DIR}"

if [ $? -ne 0 ]; then
  echo -e "${RED}构建 ${SERVICE_NAME} 镜像失败${NC}"
  echo -e "${GRAY}按Enter键退出...${NC}"
  read -r
  exit 1
fi

# 标记镜像
FULL_IMAGE_NAME="${REGISTRY}/${NAMESPACE}/${SERVICE_NAME}:${VERSION}"
echo -e "${YELLOW}标记镜像为 ${FULL_IMAGE_NAME}...${NC}"
docker tag "${SERVICE_NAME}:${VERSION}" "${FULL_IMAGE_NAME}"

if [ $? -ne 0 ]; then
  echo -e "${RED}标记 ${SERVICE_NAME} 镜像失败${NC}"
  echo -e "${GRAY}按Enter键退出...${NC}"
  read -r
  exit 1
fi

# 推送镜像
echo -e "${YELLOW}推送镜像到阿里云 (${FULL_IMAGE_NAME})...${NC}"
docker push "${FULL_IMAGE_NAME}"

if [ $? -ne 0 ]; then
  echo -e "${RED}推送 ${FULL_IMAGE_NAME} 失败${NC}"
  echo -e "${GRAY}按Enter键退出...${NC}"
  read -r
  exit 1
else
  echo -e "${GREEN}推送 ${FULL_IMAGE_NAME} 成功${NC}"
fi

# 完成提示
echo -e "\n${YELLOW}====================================================${NC}"
echo -e "${GREEN}所有操作已完成!${NC}"
echo -e "${CYAN}1. 前端代码已构建${NC}"
echo -e "${CYAN}2. dist目录已复制到Nginx目录${NC}"
echo -e "${CYAN}3. 自签名SSL证书已配置，支持HTTPS${NC}"
echo -e "${CYAN}4. Docker镜像已构建并推送: ${FULL_IMAGE_NAME}${NC}"
echo -e "${YELLOW}注意: 如果部署在生产环境，请替换SSL证书为正式证书。${NC}"
echo -e "${YELLOW}====================================================${NC}"

# 脚本结束提示
echo -e "\n${GRAY}按Enter键退出...${NC}"
read -r 